//
//  LineNodeChart.m
//  edu
//
//  Created by baobaodp on 13-11-10.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "LineNodeChart.h"
#import "Record.h"
#import "RecordField.h"
#import "SeriesItem.h"

@implementation LineNodeChart

@synthesize categoryAxisField;
@synthesize seriesType;
@synthesize valueReverse;

-(NSString *) getDataSource
{
    NSMutableString *tmp = [NSMutableString stringWithCapacity:8];
    
    [tmp appendString:@"var grandSlam = [ "];
    
    for (Record * data in self.dataSource) {
        [tmp appendString:@"{"];
        for (NSString *key in data.fields) {
            RecordField *field = [data.fields objectForKey:key];
            [tmp appendFormat:@"'%@': '%@',",field.fieldName,field.value];
        }
        [tmp appendString:@"},"];
        
    }
    [tmp appendString:@"];"];
    
    
    return tmp;
}

-(NSString *) getSeries
{
    NSMutableString *tmp = [NSMutableString stringWithCapacity:8];
    
    [tmp appendString:@"series: ["];
    
    for (SeriesItem * se in self.serieses) {
        [tmp appendString:@"{"];
        
        [tmp appendFormat:@"'field': '%@',",se.field];
        [tmp appendFormat:@"'name': '%@',",se.name];
        [tmp appendFormat:@"'visibleInLegend': %@,",@"true"];
        [tmp appendFormat:@"'visible': %@,",@"false"];
        
        [tmp appendString:@"},"];
        
    }
    [tmp appendString:@"]"];
    
    
    return tmp;
}

-(NSString *) createChartScript
{
    
    NSString * script = @"function createChart() {"
    "$('#chart').kendoChart({"
        "chartArea: {"
            //"width: 440,"
            "height: 440"
        "},"
        "theme:'silver',"
        "dataSource: {"
            "data: grandSlam"
        "},"
        "title: {"
            "text: '%@'"
        "},"
        "legend: {"
            "position: 'top',"
            "labels: {"
                "font: '18px Arial,Helvetica,sans-serif',"
            "}"
        "},"
        "seriesDefaults: {"
            "type: '%@'"
        "},"
        "%@,"
        "categoryAxis: {"
            "field: '%@',"
            "majorGridLines: {"
                "visible: false"
            "},"
            "reverse: false,"
        "},"
        "valueAxis: {"
            //"width: 440,"
            "reverse: %@"
        "},"
        "tooltip: {"
            "visible: true,"
            "template: '#= series.name #: #= value #'"
            "}"
        "});"
"}";
    return [NSString stringWithFormat:script,
            self.title,
            self.seriesType,
            [self getSeries],
            self.categoryAxisField,
            [self getValueReverse]] ;
}

-(NSString *) getValueReverse
{
    if(self.valueReverse == true){
        return @"true";
    }
    return @"false";
}

-(NSString *) customizeScript
{
    return @"var chart = $('#chart').data('kendoChart');chart.options.series[0].visible = true;chart.refresh();";
}
@end
